package com.cheyunkeji.er.fragment.auction;

import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.MyFragmentManager;
import com.cheyunkeji.er.adapter.MyFragmentPagerAdapter;
import com.cheyunkeji.er.base.BaseFragment;
import com.cheyunkeji.er.bean.MsgCountWrapper;
import com.cheyunkeji.er.utils.TabLayoutAdjustBoundUtils;
import com.cheyunkeji.er.view.TopBar;
import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;


public class AuctionFragment extends BaseFragment {


    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.tl_jinpai)
    TabLayout tlJinpai;
    @BindView(R.id.vp_frag_jinpai_content)
    ViewPager vpFragJinpaiContent;
    @BindView(R.id.tv_item_count)
    TextView tvItemCount;

    private ArrayList<String> titleList;
    private ArrayList<Fragment> fragList;
    private MyFragmentPagerAdapter adapter;

    @Override
    protected void init() {

        EventBus.getDefault().register(this);

        titleList = new ArrayList<>();
        fragList = new ArrayList<>();
        titleList.add(MyApplication.getInstance().getString(R.string.auction));
        titleList.add(MyApplication.getInstance().getString(R.string.pre_auction));
        titleList.add(MyApplication.getInstance().getString(R.string.auction_got));

        fragList.add(MyFragmentManager.getInstance().getFrag(AuctionOnProcessFragment.class));
        fragList.add(MyFragmentManager.getInstance().getFrag(AuctionPreProcessFragment.class));
        fragList.add(MyFragmentManager.getInstance().getFrag(AuctionGotFragment.class));
    }


    @Override
    protected void initView() {
        vTopbar.setTitle("竞拍");
        adapter = new MyFragmentPagerAdapter(getActivity().getSupportFragmentManager(), titleList, fragList);
        vpFragJinpaiContent.setAdapter(adapter);
        tlJinpai.setupWithViewPager(vpFragJinpaiContent);
        tlJinpai.setTabsFromPagerAdapter(adapter);
//       调整Tab头部间距 使用的是反射，在proguard文件中一定要设置以便防止混淆导致找不到方法、属性错误
        TabLayoutAdjustBoundUtils.setIndicator(getActivity(), tlJinpai, 30, 30);
        TabLayoutAdjustBoundUtils.setTabDivider(tlJinpai);
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void doInflaterView(LayoutInflater inflater, ViewGroup container) {
        contentView = inflater.inflate(R.layout.er_frag_auction_layout, container, false);
        ButterKnife.bind(this, contentView);
    }

    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgCountChanged(MsgCountWrapper wrapper){

        if(vpFragJinpaiContent.getCurrentItem() == wrapper.pageIndex ){
            setItemCount(wrapper.msgCount);
        }

    }

    public void setItemCount(int itemCount ){
        tvItemCount.setText(String.format( "共%1$s条数据" , String.valueOf(itemCount)));
    }

}
